/******************************************************************/
/*                    DoFile Replication file
	Review of Economics and Statistics
	
		Victim incentives and criminal activity: 
		Evidence from bus driver robberies in Chile   		  
			
			Author: Patricio Domínguez
			September, 2020										  */

/******************************************************************/

/* Notes: This dofile generates Tables 1, 2, and 3. 
	It also generates Figures 2, 3, and 4. */

set scheme plotplain

/******************************************************************/
/*                    Set directory structure          		      */ 
/******************************************************************/

global rep "/Users/patriciodominguez/RES_Dominguez Dropbox/Patricio Dominguez/Replication/TS"

use "$rep/dta/panel_muni_week.dta", clear

collapse (sum) crime, by(cash bus week)

g pre =0
replace pre=1 if week<42
 
g tra =0
replace tra=1 if week>=42 & week<110
 
g post =0
replace post=1 if week>=110
 
g period =0 if pre
replace period =1 if tra
replace period=2 if post

g pre_cash = pre*cash
g post_cash = post*cash
g tra_cash = tra*cash

g bus_tra =bus*tra
g bus_post = bus*post
g bus_cash =bus*cash
g tra_cash_bus =bus_cash*tra
g post_cash_bus =bus_cash*post

*** Table 1
table period cash if bus==1, c(mean crime semean crime) format(%9.2fc)
table period cash if bus==0, c(mean crime semean crime) format(%9.2fc)

*** Figure 2
twoway (line crime week if bus==1 & cash==1, lc(black) xline(42 110)), ///
		 ytitle("# Incidents", size(med)) xtitle("") ///
		tlabel(0 "2005" 52 "2006" 105 "2007" 158 "2008" 210 "2009" 262 "2010"  314 "2011", labsize(med) nogrid) xscale(range(0 313) )
		graph export "$rep/Figures/bus_evol_TS.pdf", replace
	
*** Figure 3
twoway (line crime week if bus==1 & cash==1, lc(black) xline(42 110)) ///
		(line crime week if bus==1 & cash==0, lc(gs12) lp(solid) ),  ///
		legend(label(1 "Cash") label(2 "Noncash") size(med)) ytitle("# Incidents", size(med)) xtitle("") ///
		tlabel(0 "2005" 52 "2006" 105 "2007" 158 "2008" 210 "2009" 262 "2010"  314 "2011", labsize(med) nogrid) xscale(range(0 313) )
		graph export "$rep/Figures/bus_evol.pdf", replace

*** Figure 4
twoway (line crime week if bus==0 & cash==1, lc(black) xline(42 110)) ///
		(line crime week if bus==0 & cash==0, lc(gs12) lp(solid) ),  ///
		legend(label(1 "Cash") label(2 "Noncash") size(med)) ytitle("# Incidents", size(med)) xtitle("") ///
		tlabel(0 "2005" 52 "2006" 105 "2007" 158 "2008" 210 "2009" 262 "2010"  314 "2011", labsize(med) nogrid) xscale(range(0 313) )
		graph export "$rep/Figures/streets.pdf", replace
	
*** Normalizing weekly crimes to the pre-period average
g crime_T=.
forvalues i=0/1 {
		forvalues k=0/1 {
			quietly su crime if !tra&!post&bus==`i'&cash==`k'
			replace crime_T=crime/`r(mean)' if bus==`i'&cash==`k'
	}
}

g aux = crime if bus==0 
bys cash bus week: egen mean_aux=mean(aux)
bys cash week: egen robb_ps=mean(aux)

g aux_T = crime_T if bus==0 
bys cash bus week: egen mean_aux_T=mean(aux_T)
bys cash week: egen robb_ps_T=mean(aux_T)

// Defining regression controls
global TS tra post
global DD $TS cash tra_cash post_cash
global DDD $DD bus bus_tra bus_post bus_cash tra_cash_bus post_cash_bus

*Month and year variables 
g year=2005 if week<52
replace year=2006 if week>=52 &week<104
replace year=2007 if week>=104 &week<156
replace year=2008 if week>=156 &week<208
replace year=2009 if week>=208 &week<260
replace year=2010 if week>=260 
g month = int((week)/52*12)+1 if year ==2005
replace month = int((week-52+1)/52*12)+1 if year ==2006
replace month = int((week-52*2)/52*12)+1 if year ==2007
replace month = int((week-52*3)/52*12)+1 if year ==2008
replace month = int((week-52*4)/52*12)+1 if year ==2009
replace month = int((week-52*5)/52*12)+1 if year ==2010
replace month=12 if month==13

est drop _all
qui eststo TS_1: reg crime_T $TS if cash==0 & bus==1, r
qui eststo TS_2: reg crime_T i.month $TS if cash==0 & bus==1, r
qui eststo TS_3: reg crime_T robb_ps_T i.month $TS if cash==0 & bus==1, r
qui eststo TS_4: reg crime_T $TS if cash==1 & bus==1, r
qui eststo TS_5: reg crime_T i.month $TS if cash==1 & bus==1, r
qui eststo TS_6: reg crime_T robb_ps_T i.month $TS if cash==1 & bus==1, r
qui eststo DD_1: reg crime_T $DD if bus==1, r
qui eststo DD_2: reg crime_T i.month $DD if bus==1, r
qui eststo DD_3: reg crime_T i.month $DD if bus==1 & year<2009, r
qui eststo DDD_1: reg crime_T $DDD, r
qui eststo DDD_2: reg crime_T i.month $DDD, r
qui eststo DDD_3: reg crime_T i.month $DDD if year<2009, r

*Table 2
esttab TS_*, keep (tra post) se r2
su crime if cash==0 & bus==1 & pre
su crime if cash==1 & bus==1 & pre

*Table 3
// Columns 1 and 2
esttab DD_2 DD_3, keep (tra_cash post_cash) se r2
// Columnas 3 and 4
esttab DDD_2 DDD_3, keep (tra_cash_bus post_cash_bus) se r2

